using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			Global.Hp.RegisterWithInitValue(hp =>
			{
				HpText.text = "Hp:" + Global.Hp.Value + "/" + Global.MaxHp.Value;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.MaxHp.RegisterWithInitValue(hp =>
			{
				HpText.text = "Hp:" + Global.Hp.Value + "/" + Global.MaxHp.Value;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				EnemyCount.text = "敌人:" + enemyCount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
			{
				if (Time.frameCount % 30 == 0)
				{
					var currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = currentSecondsInt % 60;
					var minutes = currentSecondsInt / 60;
					TimeText.text = "时间:" + $"{minutes:00}:{seconds:00}";
				}

			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			// please add init code here
			Global.Exp.RegisterWithInitValue(exp =>
			{
				ExpText.text = "经验值:(" + exp + "/" + Global.ExpToNextLevel() + ")";
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			Global.Level.RegisterWithInitValue(exp =>
			{
				levelText.text = "等级:" + exp;
				//Time.timeScale = 0;  调用
				//BtnUpgrade.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//这
			Global.Level.Register(exp =>
			{
				Time.timeScale = 0;
				UpgradeRoot.Show();
				AudioKit.PlaySound("Level_up");//父级
											   //	BtnUpgrade.Show();
											   //BtnSimpleDurationUpgrade.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			UpgradeRoot.Hide();
			// BtnUpgrade.Hide();
			// BtnSimpleDurationUpgrade.Hide();
			BtnUpgrade.onClick.AddListener(() =>
			{
				Time.timeScale = 1;
				Global.SimpleAbilityDamage.Value *= 1.5f;
				UpgradeRoot.Hide();
				AudioKit.PlaySound("ability_level_up");
				// BtnUpgrade.Hide();
				// BtnSimpleDurationUpgrade.Hide();
			});

			BtnSimpleDurationUpgrade.onClick.AddListener(() =>
			{
				Time.timeScale = 1;
				Global.SimpleAbilityDuration.Value *= 0.8f;
				UpgradeRoot.Hide();
				AudioKit.PlaySound("ability_level_up");
				//	BtnUpgrade.Hide();
			});
			var enemyGenerator = FindObjectOfType<EnemyGenerator>();

			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentSeconds.Value += Time.deltaTime;
				if (Global.CurrentSeconds.Value >= 60 && enemyGenerator.CurrentWave == null && EnemyGenerator.EnemyCount.Value == 0)
				{
					this.CloseSelf();
					UIKit.OpenPanel<UIGameOverPassPanel>();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);


			//	Global.Coin.Value = PlayerPrefs.GetInt("coin", 0);
			Global.Coin.RegisterWithInitValue(coin =>
			{
				//PlayerPrefs.SetInt(nameof(coin), coin);
				CoinText.text = "金币:" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}


		protected override void OnOpen(IUIData uiData = null)
		{
		}

		protected override void OnShow()
		{
		}

		protected override void OnHide()
		{
		}

		protected override void OnClose()
		{
		}
	}
}
